﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace WPFTestSolution.Pages {
    
    /**
     * https://www.bilibili.com/video/BV1bV411A7Eq?p=50
     * 050-MediaElement媒体播放器(: FrameworkElement)
     * https://www.wpfsoft.com/2023/08/31/1662.html
     */
    public partial class _50_MediaElement_Page : Page {
        
        
        private string file = string.Empty;
        
        public _50_MediaElement_Page() {
            InitializeComponent();
            this.Title = "050-MediaElement媒体播放器(: FrameworkElement)";
            
            //异步线程, 更新进度
            DispatcherTimer timer = new DispatcherTimer {
                Interval = TimeSpan.FromMilliseconds(1000.0d)
            };
            timer.Tick += (s, e) => {
                var ts = this._MediaElement.Position;
                this._ProgressBar.Value = ts.TotalMilliseconds;  //更新当前播放进度
            };
            timer.Start();
            
            this._MediaElement.MediaOpened += _MediaElement_MediaOpened;
        }

        //打开
        private void OpenMedia(object sender, RoutedEventArgs e) {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog() {
                Filter = "视频文件 (.mp4)|*.mp4",
                Multiselect = true
            };
            var result = openFileDialog.ShowDialog();
            if (result == true) {
                file = openFileDialog.FileName;
                Console.WriteLine($"fileName = {file}");
                // this._MediaElement.MediaOpened -= _MediaElement_MediaOpened;
                // this._MediaElement.MediaOpened += _MediaElement_MediaOpened;
                this._MediaElement.Source = new System.Uri(file);
                this._TextBlock.Text = file;
            }
        }

        private void _MediaElement_MediaOpened(object sender, RoutedEventArgs e) {
            /**
             * NaturalDuration: 获取介质的自然持续时间。也就是视频播放总时长。
             * HasTimeSpan == true：表示媒体有明确的时长，可以通过 NaturalDuration.TimeSpan 获取具体值。
             * HasTimeSpan == false：表示媒体时长不确定（例如直播流），此时访问 NaturalDuration.TimeSpan 会抛出异常。
             */
            if (this._MediaElement.NaturalDuration.HasTimeSpan) {
                var ts = this._MediaElement.NaturalDuration.TimeSpan;
                this._ProgressBar.Maximum = ts.TotalMilliseconds;//设置播放进度条总长
            }
        }

        //播放
        private void PlayMedia(object sender, RoutedEventArgs e) {
            if (isPlayFinished()) {
                // 重新设置MediaElement的Position为0，否则不能重新开始播放
                this._MediaElement.Position = TimeSpan.Zero;
            }
            this._MediaElement.Play();
            this._Border.Visibility = Visibility.Collapsed;
        }

        //暂停
        private void StopMedia(object sender, RoutedEventArgs e) {
            this._MediaElement.Pause();
        }

        //后退10秒
        private void BackMedia(object sender, RoutedEventArgs e) {
            this._MediaElement.Position = _MediaElement.Position - TimeSpan.FromSeconds(10);
        }

        //快进10秒
        private void ForwardMedia(object sender, RoutedEventArgs e) {
            this._MediaElement.Position = _MediaElement.Position + TimeSpan.FromSeconds(10);
        }

        //拉进度
        private void _Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) {
            _MediaElement.Volume = _Slider.Value / 100;
        }

        //MediaElement 没有 isPlaying 方法...
        private bool isPlayFinished() {
            // 方法2: 通过 Position 和 NaturalDuration 间接判断
            return _MediaElement.NaturalDuration.HasTimeSpan &&
                   _MediaElement.Position >= _MediaElement.NaturalDuration.TimeSpan
                   ;
                   //&& !_MediaElement.IsPaused; // 注意: IsPaused 也不是标准属性，需自行维护
        }
    }
}